package com.sunday.common.mq.rocket.brave.study.apache.e2_PULL;

import com.sunday.common.mq.rocket.brave.study.apache.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 消费消息
 * https://github.com/apache/rocketmq-spring/wiki/Consume-Message
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class PullApi {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private static final String topic = "your_topic";

    @GetMapping("send")
    public void send() {

        User user = User.builder().userName("john").userAge(33).build();

        rocketMQTemplate.syncSendOrderly(topic, user, user.getUserAge().toString());
        rocketMQTemplate.syncSendOrderly(topic, user, user.getUserAge().toString());
        rocketMQTemplate.syncSendOrderly(topic, user, user.getUserAge().toString());

    }

    @GetMapping("pull")
    public void pull() {
        List<User> list;
        while (!(list = rocketMQTemplate.receive(User.class)).isEmpty()) {
            list.stream().forEach(user -> log.info("{}", user));
        }
    }

}